package middle;

import java.util.Arrays;

public class Solution_318 {

    public static void main(String[] args) {
        Solution_318 model = new Solution_318();
        System.out.println(model.maxProduct(new String[]{"abcw","baz","foo","bar","xtfn","abcdef"}));
        System.out.println(model.maxProduct(new String[]{"a","ab","abc","d","cd","bcd","abcd"}));
        System.out.println(model.maxProduct(new String[]{"a","aa","aaa","aaaa"}));
    }

    public int maxProduct(String[] words) {
        int[] arrays = new int[words.length];
        boolean[] flags = new boolean[26];
        for (int i = 0; i < words.length; i++) {
            Arrays.fill(flags, false);
            for (int j = 0; j < words[i].length(); j++) {
                int offset = words[i].charAt(j) - 'a';
                if (!flags[offset]) {
                    flags[offset] = true;
                }
            }
            int num = 0;
            for (int j = 0; j < flags.length; j++) {
                num <<= 1;
                if (flags[j]) {
                    num += 1;
                }
            }
            arrays[i] = num;
//            System.out.println(Integer.toBinaryString(num));
        }
        int max = 0;
        for (int i = 0; i < arrays.length; i++) {
            for (int j = i+1; j < arrays.length; j++) {
                if ((arrays[i] & arrays[j]) == 0) {
                    int length = words[i].length() * words[j].length();
                    if (length > max) {
                        max = length;
                    }
                }
            }
        }
        return max;
    }
}
